﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Honestysoft.Baseframework.WebUI;
using System.IO;

namespace Honestysoft.Financing.WebUI
{
    public class Template
    {
        /// <summary>
        /// 模板文件
        /// </summary>
        public abstract class TemplateFile
        {
            protected string _TargetFileName;

            public TemplateFile(string targetFileName)
            {
                _TargetFileName = targetFileName;
            }

            /// <summary>
            /// 模板文件所在目录
            /// </summary>
            public virtual string TemplateDir
            {
                get { return ""; }
            }
            /// <summary>
            /// 模板文件名
            /// </summary>
            public virtual string TemplateFileName
            {
                get { return ""; }
            }
            /// <summary>
            /// 模板文件目录
            /// </summary>
            public string TemplateDirMapPath
            {
                get
                {
                    try
                    {
                        return HttpContext.Current.Server.MapPath(Globals.Home + TemplateDir);
                    }
                    catch (Exception ex)
                    {
                        if (TemplateDir == "") { throw new Honestysoft.BaseLibrary.ExceptionHandling.BaseException("没有设置模板文件所在目录！", ex); }
                        return "";
                    }
                }
            }

            /// <summary>
            /// 模板文件物理路径
            /// </summary>
            public string TemplateFileMapPath
            {
                get
                {
                    try
                    {
                        return HttpContext.Current.Server.MapPath(Globals.Home + TemplateDir + "/" + TemplateFileName);
                    }
                    catch(Exception ex)
                    {
                        if (TemplateDir == "") { throw new Honestysoft.BaseLibrary.ExceptionHandling.BaseException("没有设置模板文件所在目录！",ex); }
                        if (TemplateFileName == "") { throw new Honestysoft.BaseLibrary.ExceptionHandling.BaseException("没有设置模板文件！", ex); }
                        return "";
                    }
                }
            }
            /// <summary>
            /// 目标目录
            /// </summary>
            public virtual string TargetDir
            {
                get { return ""; }
            }

            /// <summary>
            /// 目标文件目录
            /// </summary>
            public string TargetDirMapPath
            {
                get
                {
                    try
                    {
                        return HttpContext.Current.Server.MapPath(Globals.Home + TargetDir);
                    }
                    catch (Exception ex)
                    {
                        if (TargetDir == "") { throw new Honestysoft.BaseLibrary.ExceptionHandling.BaseException("没有设置目标文件所在目录！", ex); }
                        return "";
                    }
                }
            }
            /// <summary>
            /// 目标文件物理路径
            /// </summary>
            public string TargetFileMapPath
            {
                get
                {
                    try
                    {
                        return HttpContext.Current.Server.MapPath(Globals.Home + TargetDir + "/" + _TargetFileName);
                    }
                    catch (Exception ex)
                    {
                        if (TargetDir == "") { throw new Honestysoft.BaseLibrary.ExceptionHandling.BaseException("没有设置目标文件所在目录！", ex); }
                        if (_TargetFileName == "") { throw new Honestysoft.BaseLibrary.ExceptionHandling.BaseException("没有设置目标文件名称！", ex); }
                        return "";
                    }
                }
            }
            /// <summary>
            /// 初始化目标文件目录
            /// </summary>
            public virtual void InitTargetDir()
            {
                if (!Directory.Exists(TargetDirMapPath))
                {
                    Directory.CreateDirectory(TargetDirMapPath);
                }
            }
            /// <summary>
            /// 删除已生成的目标文件
            /// </summary>
            public virtual void DeleteTargetFile()
            {
                if (File.Exists(TargetFileMapPath))
                {
                    File.Delete(TargetFileMapPath);
                }
            }
        }

        /// <summary>
        /// 计划进度示意图
        /// </summary>
        public class JD : TemplateFile
        {
            public JD(string targetFileName)
                : base(targetFileName)
            {
            }

            public override string TemplateDir
            {
                get
                {
                    return "TemplateFile";
                }
            }

            public override string TemplateFileName
            {
                get
                {
                    return "JDTemplate.xls";
                }
            }

            public override string TargetDir
            {
                get
                {
                    return "Reports/JD";
                }
            }
        }
        /// <summary>
        /// 项目投资计划完成情况及工程进度和资金拨付比例示意图
        /// </summary>
        public class JDZF : TemplateFile
        {
            public JDZF(string targetFileName)
                : base(targetFileName)
            {
            }

            public override string TemplateDir
            {
                get
                {
                    return "TemplateFile";
                }
            }

            public override string TemplateFileName
            {
                get
                {
                    return "JDZFTemplate.xls";
                }
            }

            public override string TargetDir
            {
                get
                {
                    return "Reports/JDZF";
                }
            }
        }
    }
}